#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using FreeOnlineApplication.Class.Repository;
using FreeOnlineApplication.Class.Entity;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Mail;

#endregion

namespace FreeOnlineApplication
{
    public partial class RegisterForm : Form
    {
        UnitRepository m_unitRep;

        public RegisterForm(IMainForm mainForm)
        {
            InitializeComponent();
            m_unitRep = new UnitRepository(mainForm.Connection);
            string result = "";
            Assembly ass = Assembly.GetExecutingAssembly();
            Stream stream = ass.GetManifestResourceStream("FreeOnlineApplication.EULA.txt");
            using (StreamReader reader = new StreamReader(stream))
            {
                result = reader.ReadToEnd();
            }
            textBoxEULA.Text = result;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            ClearError();
            if (isValid())
            {
                Unit unit = new Unit();
                unit.ADDRESS = textBoxAddress.Text;
                unit.EMAIL = textBoxEmail.Text;
                unit.NAME = textBoxName.Text;
                unit.WEBSITE = textBoxWebsite.Text;
                User user = m_unitRep.InsertReturnAdminUser(unit);

//                MessageBox.Show(@"New Hotel has been registered, your admin login : ADMIN
//                    , password : " + user.PASSWORD + " , unit Code : " + unit.CODE + " , Thanks for joining! ", "Welcome",
//                             new EventHandler(CloseForm));

                SendMail(unit.EMAIL, "Registering to Hotel Management System is sucessfully",
                    @"New Hotel has been registered, your admin login : ADMIN
                    , password : " + user.PASSWORD + " , unit Code : " + unit.CODE + " , Thanks for joining! ");
                MessageBox.Show(@"Confirmation has been send to your email, please kindly check.", "Welcome",
                             new EventHandler(CloseForm));

            }
        }
        void CloseForm(object sender, EventArgs e)
        {
            this.Close();
        }
        private bool isValid()
        {
            bool b = textBoxName.Text == "";
            bool c = textBoxAddress.Text == "";
            bool d = textBoxWebsite.Text == "";
            bool e = textBoxEmail.Text == "";
            bool f = !IsValidEmailAddress(textBoxEmail.Text.Trim());
            bool g = !(m_unitRep.GetByEmail(textBoxEmail.Text.Trim()) == null);

            if (b) errorProvider1.SetError(textBoxName, "Can not Empty");
            if (c) errorProvider1.SetError(textBoxAddress, "Can not Empty");
            if (d) errorProvider1.SetError(textBoxWebsite, "Can not Empty");
            if (e) errorProvider1.SetError(textBoxEmail, "Can not Empty");
            if (f) errorProvider1.SetError(textBoxEmail, "Email is not valid");
            if (g) errorProvider1.SetError(textBoxEmail, "This email already registered");

            return !b && !c && !d && !e && !f && !g;
        }
        public bool IsValidEmailAddress(string EmailAddress)
        {
            Regex regEmail = new Regex(@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (regEmail.IsMatch(EmailAddress)) ///in Email address write user email address
                return true;
            return false;
        }
        private void ClearError()
        {
            errorProvider1.SetError(textBoxName, "");
            errorProvider1.SetError(textBoxAddress, "");
            errorProvider1.SetError(textBoxWebsite, "");
            errorProvider1.SetError(textBoxEmail, "");
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            buttonApply.Enabled = checkBox1.Checked;
        }

        private void RegisterForm_Load(object sender, EventArgs e)
        {

        }
        public static bool SendMail(string to, string subject, string message)
        {
            try
            {
                NetworkCredential loginInfo = new NetworkCredential("admin@softwarebatam.com", "Suzuki2011");
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("admin@softwarebatam.com");
                msg.To.Add(new MailAddress(to));
                msg.Subject = subject;
                msg.Body = message;
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient("smtp.gmail.com");
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = loginInfo;
                client.Send(msg);

                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}